package ui.activity.mine.presenter;


import com.google.gson.Gson;

import javax.inject.Inject;

import base.BaseBiz;
import model.PersonalCenterBean;
import model.PersonalCenterParamBean;
import model.PersonalIconBean;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.mine.biz.PersonalCenterBiz;
import ui.activity.mine.contract.PersonalCenterContract;
import ui.model.UploadImageBean;


/**
 * Created by chenjian on 19/2/26
 */

public class PersonalCenterPresenter implements PersonalCenterContract.Presenter {

    PersonalCenterContract.View view;
    PersonalCenterBiz personalCenterBiz;


    @Inject
    public PersonalCenterPresenter(PersonalCenterContract.View view) {
        this.view = view;
    }

    @Override
    public void setBiz(BaseBiz biz) {
        personalCenterBiz = (PersonalCenterBiz) biz;
    }


    @Override
    public void getUserInfo(String id) {
        view.loading(true);
        PersonalCenterParamBean p = new PersonalCenterParamBean();
        p.setCustomId(id);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(p));
        personalCenterBiz.getUserInfo(requestBody, new BaseBiz.Callback<PersonalCenterBean.DataBean>() {
            @Override
            public void onSuccess(PersonalCenterBean.DataBean info) {
                view.upDateUI(info);
                view.loading(false);
            }

            @Override
            public void onFailure(PersonalCenterBean.DataBean info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void toLoadImage(String id, String imagePath) {

        String base64 = "data:image/png;base64," + Utils.FileUtil.compress(imagePath);
        PersonalIconBean p = new PersonalIconBean();
        p.setCustomId(id);
        p.setHeadimgData(base64);
        view.loading(true);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(p));
        personalCenterBiz.updateImage(requestBody, new BaseBiz.Callback<UploadImageBean>() {
            @Override
            public void onSuccess(UploadImageBean info) {
                view.showMsg("上传头像成功");
                view.upDateImage(info);
                view.loading(false);
            }

            @Override
            public void onFailure(UploadImageBean info) {
                view.loading(false);
            }
        });
    }


}
